import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.StringTokenizer;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.jsoup.Jsoup;

public class CheckXls {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Workbook wb = WorkbookFactory.create(new FileInputStream("test.xls") );			
			Sheet sheet = wb.getSheet("Products");

			for (int i = 1; i < sheet.getLastRowNum(); i++) {
			//for (int i = 1; i < Math.min(100, sheet.getLastRowNum()); i++) {
				Row row = sheet.getRow(i);
				Cell cellDesc = row.getCell(32);
				Cell cellName = row.getCell(1);
				Cell cellId = row.getCell(0);

				String desc = cellDesc.getStringCellValue();
				String name = cellName.getStringCellValue();
				String id = Double.toString(cellId.getNumericCellValue());

				// 1. törlés az elejéről
//				desc = desc.replaceAll("<p>\\s*&nbsp;\\s*</p>", "");
//				desc = desc.replaceAll("\\s*&nbsp;\\s*", "");
//				cellDesc.setCellValue(desc);
//				System.out.println("-------------------------------------------------------------------------");
//				System.out.println(desc);

				// 2. tartalmaz-e style=
//				 if(desc.contains("style=")) {
//					 System.out.println(id);
//				 }

				// 3. tartalmaz vinoportot
//				 if(desc.toLowerCase().contains("bormagazin")) {//bormagazin, vinoport
//					 System.out.println(id);
//				 }

				// 4. HTML entity ref ellenőrés: név, gyártó leírás. Dúzsi szar
				// a keresésnél.

//				if (desc.toLowerCase().contains("0,5")) {
////					//desc = desc.replace("&nbsp;", " ");
////					//cellDesc.setCellValue(desc);
////					
//					System.out.println("-------------------------------------------------------------------------");
//					System.out.println(id);
//					System.out.println(name);
//				}

				// 5. üres manufacturerek keresése (szemmel)
				// 6. qty
				// 7. nem jó kategorizálás keresée (szemmel)
				
				
				// 8. meta leírás készítés a leírásból.
//				if(desc != null) {
//					try {
//						row.getCell(33).setCellValue(Jsoup.parse(desc).text());
//					} catch (Exception e) {
//						System.out.println(id);
//					}
//				}
								
				// utolso: szemmel ellenörzés
//				System.out
//						.println("--- "
//								+ id
//								+ " ----------------------------------------------------------------------");
//				System.out
//						.println(addLinebreaks(Jsoup.parse(desc).text(), 120));
			}
			
//			FileOutputStream fileOut = new FileOutputStream("test2.xls");
//			wb.write(fileOut);
//			fileOut.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static String addLinebreaks(String input, int maxLineLength) {
		StringTokenizer tok = new StringTokenizer(input, " ");
		StringBuilder output = new StringBuilder(input.length());
		int lineLen = 0;
		while (tok.hasMoreTokens()) {
			String word = tok.nextToken();

			if (lineLen + word.length() > maxLineLength) {
				output.append("\n");
				lineLen = 0;
			}
			output.append(word + " ");
			lineLen += word.length();
		}

		return output.toString();
	}
}
